﻿using System;
using System.Text;
using System.Collections.Generic;
using DDotNet.ORM.DataFactory;
using DDotNet.ORM.DataFactory.DirectAccess;
using DDotNet.ORM.DataFactory.DirectAccess.Enum;
using DDotNet.ORM.DataFactory.DirectAccess.QueryParameter;
using DDotNet.ORM.DataFactory.Enum;
using DDotNet.ORM.DataFactory.Interface;
using GenericTests.Entities;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace GenericTests
{
    /// <summary>
    /// Summary description for DataBaseFactory
    /// </summary>
    [TestClass]
    public class LinkEntity_DataBaseFactory
    {
        public LinkEntity_DataBaseFactory()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod()]
        public void InsertLinks()
        {
            // Cria Objetos
            ICrudFactory factory;
            Customer newCustomer = new Customer();
            Shop newCustomerShopI = new Shop();
            ShopItem newShopItem = new ShopItem();
            DirectAccessQueryFactory directAccessQueryFactory;

            Customer oldCustomer;
            
            // Configura Customer
            newCustomer.Name = "BlaBlabla";
            
            // Configura Shops
            newCustomerShopI.ShopDate = DateTime.Now;
            newCustomer.Shops.Add(newCustomerShopI);

            // Configura Shop Item
            newShopItem.Value = 256.99;
            newShopItem.ProductName = "Silver Arrows";
            newCustomerShopI.ShopItens.Add(newShopItem);
            
            try
            {
                using (factory = new CRUDFactory())
                {
                    // Atualiza
                    factory.Update(newCustomer);
                }

                // Verifica se Trouxe um ID
                Assert.IsTrue(newCustomer.ID > 0, "Não foi possível inserir ou não foi possível recuperar o ID Identity para uma Entidade de Link");
                
                // Recupera Registro e Verifica se Bate os Campos
                using (factory = new CRUDFactory())
                {
                    directAccessQueryFactory = new DirectAccessQueryFactory(factory);
                    // Recupera
                    oldCustomer = (Customer)directAccessQueryFactory.SelectItemByQuery(typeof(Customer), new GenericQueryParameter("ID", GenericComparerMode.Equals, newCustomer.ID));
                }
                
                // Compara os Valores
                Assert.AreEqual(oldCustomer.Name, newCustomer.Name, "Os Valores Inseridos não são iguais aos Valores Recuperados");
            }
            catch (Exception e)
            {
                Assert.Fail(e.Message);
            }
        }

        [TestMethod()]
        public void UpdateLinks()
        {
            // Cria Objetos
            ICrudFactory factory;
            Customer customer = new Customer();
            Shop shopI = new Shop();
            ShopItem shopItemI = new ShopItem();

            // Configura Customer
            customer.Name = "BlaBlabla";

            // Configura Shops
            shopI.ShopDate = DateTime.Now;
            customer.Shops.Add(shopI);

            // Configura Shop Item
            shopItemI.Value = 256.99;
            shopItemI.ProductName = "Silver Arrows";
            shopI.ShopItens.Add(shopItemI);

            try
            {
                using (factory = new CRUDFactory())
                {
                    // Insere
                    factory.Update(customer);
                    
                    // Muda Valores
                    customer.Name = "XPTO";
                    customer.Shops[0].ShopDate = DateTime.Now.AddDays(1);
                    customer.Shops[0].ShopItens[0].Value = 123456.7890;
                    
                    // Atualiza Novamente
                    factory.Update(customer);
                }
            }
            catch (Exception e)
            {
                Assert.Fail(e.Message);
            }
        }

        [TestMethod()]
        public void DeleteLinks()
        {
            // Cria Objetos
            ICrudFactory factory;
            Customer newCustomer = new Customer();
            Shop newCustomerShopI = new Shop();
            ShopItem newShopItem = new ShopItem();
            DirectAccessQueryFactory directAccessQueryFactory;
            
            // Configura Customer
            newCustomer.Name = "BlaBlabla333";

            // Configura Shops
            newCustomerShopI.ShopDate = DateTime.Now;
            newCustomer.Shops.Add(newCustomerShopI);

            // Configura Shop Item
            newShopItem.Value = 256.99;
            newShopItem.ProductName = "Silver Arrows333";
            newCustomerShopI.ShopItens.Add(newShopItem);

            try
            {
                using (factory = new CRUDFactory())
                {
                    // Insere
                    factory.Update(newCustomer);
                }

                // Deleta
                using (factory = new CRUDFactory())
                {
                    directAccessQueryFactory = new DirectAccessQueryFactory(factory);
                    // Recupera
                    newCustomer.MarkDeleted();
                    factory.Update(newCustomer);

                    // Verifica se não Acha
                    Assert.IsNull(directAccessQueryFactory.SelectItemByQuery(typeof(Customer), new GenericQueryParameter("ID", GenericComparerMode.Equals, newCustomer.ID)), "A Exclusão de Link não Funciona");
                }
            }
            catch (Exception e)
            {
                Assert.Fail(e.Message);
            }
        }
    }
}
